import { createFilterColumns, createTableRequestParams } from '@/utils/table';
import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { PageContainer, ProTable } from '@ant-design/pro-components';
import { Button } from 'antd';
import { useMemo, useRef, useState } from 'react';
import api from './request';

function PointsTransfter() {
  const actionRef = useRef<ActionType>();

  const [filterList, setFilterList] = useState<Requestor.FilterResponse[]>([]);

  // request
  const getFilterList = async () => {
    // const filterList = await api.getFilterList();
    const filterList: Requestor.FilterResponse[] = [];
    setFilterList(filterList);
    return filterList;
  };
  const getListRequest = (
    params: Requestor.TableParams,
    filterList: Requestor.FilterResponse[],
  ) => {
    const requestParams = createTableRequestParams(params, filterList);
    return api.getList({ ...requestParams, sortField: 'transferTime' });
  };
  const getList = async (params: Requestor.TableParams) => {
    if (!filterList || !filterList.length) {
      const filterList = await getFilterList();
      return getListRequest(params, filterList);
    } else {
      return getListRequest(params, filterList);
    }
  };

  const columns = useMemo(() => {
    const filterColumns = createFilterColumns<PointsTransferApi.ListResponse>(filterList);
    const tableColumns: ProColumns<PointsTransferApi.ListResponse>[] = [
      {
        title: '转出用户名',
        dataIndex: 'fromUsername',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '转入用户名',
        dataIndex: 'toUsername',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '转让积分',
        dataIndex: 'points',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '转让时间',
        dataIndex: 'transferTime',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      {
        title: '备注',
        dataIndex: 'remarks',
        ellipsis: true,
        hideInSearch: true,
        align: 'center',
      },
      // {
      //   title: '操作',
      //   hideInSearch: true,
      //   align: 'center',
      //   width: '20%',
      //   render(_, record) {
      //     return (
      //       <Space>
      //         <Typography.Link>
      //           <Form data={record} onSuccess={() => actionRef.current?.reload()} />
      //         </Typography.Link>
      //       </Space>
      //     );
      //   },
      // },
    ];
    return filterColumns.concat(tableColumns);
  }, [filterList]);
  return (
    <PageContainer>
      <ProTable
        bordered
        size="small"
        rowKey="id"
        options={false}
        actionRef={actionRef}
        pagination={{ defaultPageSize: 10, showSizeChanger: true }}
        toolBarRender={() => []}
        search={{
          optionRender: () => {
            return [
              <Button key="search" type="primary" onClick={() => actionRef.current?.reload()}>
                查询
              </Button>,
            ];
          },
        }}
        columns={columns}
        request={getList}
      />
    </PageContainer>
  );
}

export default PointsTransfter;
